package itheima.com.demo;

import java.io.*;
import java.net.Socket;
import java.util.UUID;

public class ThreadPool implements Runnable {
    private Socket accepts;

    public ThreadPool(Socket accept) {
        this.accepts=accept;
    }

    @Override
    public void run() {
        BufferedOutputStream bos =null;
        try {
            System.out.println("欢迎光临，请上传文件");
            BufferedInputStream bis = new BufferedInputStream(accepts.getInputStream());

            bos = new BufferedOutputStream(new FileOutputStream("WorkDemo\\ccc"+ UUID.randomUUID().toString()+".jpg"));

            int b;
            while ((b=bis.read())!=-1){
                bos.write((char)b);
            }

            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(accepts.getOutputStream()));
            bw.write("上传成功");
            bw.newLine();
            bw.flush();

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(bos!=null){
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }


            if(accepts!=null){
                try {
                    accepts.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }
}
